export function maxLevelSum(root: TreeNode | null): number {
  if (!root) return 0;
  const tempArr: TreeNode[] = [root];
  let res = 1;
  let deep = 0;
  let maxsum = root.val;
  while (tempArr.length) {
    const len = tempArr.length;
    deep++;
    let sumTemp = 0;
    for (let index = 0; index < len; index++) {
      const node = tempArr.shift() as TreeNode;
      if (node) {
        sumTemp = sumTemp + node?.val;
        if (node.left) tempArr.push(node.left);
        if (node.right) tempArr.push(node.right);
      }
    }
    if (maxsum < sumTemp) {
      maxsum = sumTemp;
      res = deep;
    }
  }
  return res;
}
